//***************************************************************************************************************
//Author: Alfredo Scoppa 
//file: index.js
//***************************************************************************************************************
var codigos = new Array();

$(function() {
	var fiubaPath = new String("http://intra.fi.uba.ar/insc/consultas/");
	var local = new String(window.location);

	local = local.substr(0,local.length-10);
	
	$("#departments").load("http://intra.fi.uba.ar/insc/consultas/cursos.jsp .tablaitem a:odd",
					function () {
						$("#departments a").each(function(item){
													var link = $(this).attr("href");
													codigos.push(link.replace(local,fiubaPath));
													$(this).attr("href",codigos[item]);
													$(this).before("<br><br>");													
												})
	});	
	
	$("#departmentInfo").click(function(){
		var department = $("#department").val();
		alert(getDepartmentNumber(department)+" ,"+getDepartmentName(department));
	})
	
	$("#save").click(function(){
				for (var department=1; department<getDepartmentsCount()+1; department++)
				{
					$("#save").attr("disabled","true");
					
					$.ajax(
					{
						type: "GET",
						url: "php/loadDepartments.php",
						data: "number="+getDepartmentNumber(department)+"&name="+getDepartmentName(department),
						dataType: 'text',
						error: function(obj1, e, obj2)
						{
							alert("Se produjo un error : "+ e);
						}
					}); 
				}
	});
	
	$("#save").ajaxComplete(function(){
		$("#save").removeAttr("disabled");
	});
	
	
	
	
});

function getDepartmentName(position){
	return new String($("#departments a").slice(position-1,position).text());
}

function getDepartmentNumber(position){
	var departmentLink = codigos[position-1].split("=");
	return departmentLink[1].substr(0,2);
}

function getDepartmentsCount(){
	return codigos.length;
}
